<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="姓名" prop="userName">
    <el-input type="text" v-model="ruleForm.userName" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="userPwd">
    <el-input type="password" v-model="ruleForm.userPwd" autocomplete="off"></el-input>
  </el-form-item>

  <el-form-item label="电话" prop="userTel">
    <el-input v-model.number="ruleForm.userTel"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="userEmail">
    <el-input type="text" v-model="ruleForm.userEmail" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</template>

<script>
import {addUser} from '../services/UserService.js'
export default {
    data() {
      var checkTel = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('电话不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value.toString().length !== 11 ) {
              callback(new Error('电话号码必须是11位'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.userPwd !== '') {
            // this.$refs.ruleForm.validateField('userPwd');
          }
          callback();
        }
      };
      var checkEmail = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入邮箱'));
        } else {
          var regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
                if (this.ruleForm.userEmail != "" && !regEmail.test(this.ruleForm.userEmail)) {
            callback(new Error('邮箱格式不正确'));
          }
          callback();
        }
      };
      var validateUserName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入姓名'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          userName: '',
          userPwd: '',
          userTel: '',
          userEmail:'',
        },
        rules: {
          userPwd: [
            { validator: validatePass, trigger: 'blur' }
          ],
          userName: [
            { validator: validateUserName, trigger: 'blur' }
          ],
          userTel: [
            { validator: checkTel, trigger: 'blur' }
          ],
          userEmail:[{validator: checkEmail, trigger:'blur'}]
        }
      };
    },
    methods: {
       submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addUser(this.ruleForm).then((resp) => {
              console.log(resp)
              if(resp.data == "true"){
                this.$router.push({name:"User"});
              }else{
                alert("服务器正忙，添加失败");
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    }
  }

</script>

<style>

</style>